2  Data Structures

There are 5 main data structures in R. They are:

  1. vectors

  2. matrix

  3. array

  4. data frame

  5. list

2.1 Vectors

  1. One dimensional data object.

  2. Homogeneous data structure. That means data in a vector must only be one type or mode (numeric, character, or logical). You can not mix different types of data. If you try to mix different types of data, R will automatically convert them into one type.

2.2 Creating Vectors

2.3 Coercion

When you try to include different types they will be coerced to the most flexible type.

2.4 Creating vectors

2.5 Functions that can be used to inspect vectors

2.6 Exercise

  1. Write R codes to create following vectors. If you see pattern in the data, use vector simplification methods.
[1] 1990 1992 1934 1957 1970 2000 2005
 [1] 3 6 9 3 6 9 3 6 9 3 6 9 3 6 9
 [1] 3 3 3 3 3 6 6 6 6 6 9 9 9 9 9
 [1] 3 3 3 3 3 6 6 6 6 6 9 9 9 9 9 3 3 3 3 3 6 6 6 6 6 9 9 9 9 9
 [1]  1  4  7 10 13 16 19 22 25 28 31 34
  [1] 0.1000000 0.1020202 0.1040404 0.1060606 0.1080808 0.1101010 0.1121212
  [8] 0.1141414 0.1161616 0.1181818 0.1202020 0.1222222 0.1242424 0.1262626
 [15] 0.1282828 0.1303030 0.1323232 0.1343434 0.1363636 0.1383838 0.1404040
 [22] 0.1424242 0.1444444 0.1464646 0.1484848 0.1505051 0.1525253 0.1545455
 [29] 0.1565657 0.1585859 0.1606061 0.1626263 0.1646465 0.1666667 0.1686869
 [36] 0.1707071 0.1727273 0.1747475 0.1767677 0.1787879 0.1808081 0.1828283
 [43] 0.1848485 0.1868687 0.1888889 0.1909091 0.1929293 0.1949495 0.1969697
 [50] 0.1989899 0.2010101 0.2030303 0.2050505 0.2070707 0.2090909 0.2111111
 [57] 0.2131313 0.2151515 0.2171717 0.2191919 0.2212121 0.2232323 0.2252525
 [64] 0.2272727 0.2292929 0.2313131 0.2333333 0.2353535 0.2373737 0.2393939
 [71] 0.2414141 0.2434343 0.2454545 0.2474747 0.2494949 0.2515152 0.2535354
 [78] 0.2555556 0.2575758 0.2595960 0.2616162 0.2636364 0.2656566 0.2676768
 [85] 0.2696970 0.2717172 0.2737374 0.2757576 0.2777778 0.2797980 0.2818182
 [92] 0.2838384 0.2858586 0.2878788 0.2898990 0.2919192 0.2939394 0.2959596
 [99] 0.2979798 0.3000000
 [1] -0.5  0.5  1.5  2.5  3.5  4.5  5.5  6.5  7.5  8.5  9.5 10.5
 [1]  2  4  6  8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
[26] 52 54 56 58 60 62 64 66 68 70 72
  1. Use typeof() function to check the R storage mode of the following vectors and class() to check the class type of the vector.
logical_vector <- c(TRUE, FALSE, TRUE, FALSE)
integer_vector <- c(1L, 2L, 3L, 4L)
double_vector <- c(1.1, 2.2, 3.3, 4.4)
complex_vector <- c(1+1i, 2+2i, 3+3i, 4+4i)
character_vector <- c("a", "b", "c", "d")
null_vector <- NULL
time_data <- 1996:2006
time_series_data <- ts(1996:2006)
  1. Create the vector (3, 3, 3, . . . 3, 6, 6, . . . 6, 9, 9, 9, . . . 9), where there are 10 occurrences of 3, 20 occurrences of 6 and 30 occurrences of 9.

  2. Find the value of the following expression.

  1. \(\sum_{i=1}^{100}i\)

  2. \(\sum_{i=1}^{100}i^2\)

  1. Generate a sequence using the code seq(from=1, to=10, by=1). What other ways can you generate the same sequence?

  2. Create a vector to hold population values, and label each element with the corresponding province name. The plot will display population values when hovered over.